#include <stdio.h>

int compare(int * a1,int * a2){
    return a1[2]>a2[2]?1:0;
}

void sort(int** array,int n)
{
    int i,j;
    int** queue=new int*[n];
    int start=0;
    int end=1;
    queue[start]=new int[2];
    queue[start][0]=0;
    queue[start][1]=n;
    //开始快排
    while (start<end)
    {
        int s=queue[start][0];
        int e=queue[start][1];
        
        i=s;
        j=e-1;
        int tmpIndex=j;
        int* tmp=array[tmpIndex];
        while (i<j)
        {
            if(tmpIndex==j)
            {
                //前面大于后面
                if(compare(array[i],tmp))
                {
                    array[tmpIndex]=array[i];
                    tmpIndex=i;
                    // tmp=array[tmpIndex];
                }
            }
            else
            {
                //前面大于后面
                if(compare(tmp,array[j]))
                {
                    array[tmpIndex]=array[j];
                    tmpIndex=j;
                    // tmp=array[tmpIndex];
                }
            }

            if(tmpIndex==j)
            {
                i++;
            }
            else{
                j--;
            }

            
        }

        array[i]=tmp;

        start++;
        if(s<i)
        {
            end++;
            queue[end-1]=new int[2];
            queue[end-1][0]=s;
            queue[end-1][1]=i;
        }

        if(i+1<e)
        {
            end++;
            queue[end-1]=new int[2];
            queue[end-1][0]=i+1;
            queue[end-1][1]=e;
        }
    }

    for(i=0;i<end;i++)
    {
        delete queue[i];
    }
    delete queue;
}

void printArray(int** input,int n)
{
    for(int i=0;i<n;i++)
    {
        printf("%d\t%d\t%d\n",input[i][0],input[i][1],input[i][2]);
    }
    printf("\n");
}

//找到根节点 并把中间节点指向根节点
int findAndReplace(int * search,int index,int n)
{
    if(search[index]==index)
    {
        return index;
    }

    int i=search[index];
    int* jl=new int[n];
    int len=0;
    while (search[i]!=i)
    {
        i=search[i];
        jl[len]=i;
        len++;
    }

    //修改中间节点的指向
    for(int j=0;j<len;j++)
    {
        search[jl[j]]=i;
    }
    delete jl;
    return i;
}


int main(){
    int n,m;
    int** input;
    int i;

    scanf("%d %d",&n,&m);
    
    input=new int*[m];
    for(i=0;i<m;i++)
    {
        input[i]=new int[3];
        scanf("%d %d %d",&input[i][0],&input[i][1],&input[i][2]);
        input[i][0]--;
        input[i][1]--;
    }

    sort(input,m);

    // printArray(input,m);
    int* search=new int[n];
    for(i=0;i<n;i++)
    {
        search[i]=i;
    }

    int result=-1;
    int tmpLen=n;
    for(i=0;i<m;i++)
    {
        int a=findAndReplace(search,input[i][0],n);
        int b=findAndReplace(search,input[i][1],n);
        //合并
        if(a!=b)
        {
            search[b]=a;
            tmpLen--;
            if(tmpLen<=1)
            {
                result=input[i][2];
            }
        }
    }

    printf("%d\n",result);

    for(i=0;i<m;i++)
    {
        delete input[i];
    }
    delete input;
    delete search;
}
